#ifndef H_VPASS_INCLUDED
#define H_VPASS_INCLUDED

#define INSTANCE_ID_MASK     1023
#define INSTANCE_ID_BITS     10

#include "UnityShaderVariables.cginc"
#include "UnityShaderUtilities.cginc"
#include "UnityInstancing.cginc"

struct Attributes
{
    float4 posL : POSITION;
    float3 normalL : NORMAL;
    float2 texC : TEXCOORD0;
    uint vID : SV_VertexID;
    //UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct Varyings
{
    float4 posH : SV_POSITION;
    float3 normalW : NORMAL;
    float2 texC : TEXCOORD0;
    float3 posW : TEXCOORD1;
    uint vID : TEXCOORD2;
    //UNITY_VERTEX_INPUT_INSTANCE_ID
};

/*
UNITY_INSTANCING_BUFFER_START(HPerInstanceProp)
UNITY_DEFINE_INSTANCED_PROP(float4,  _Color)
UNITY_INSTANCING_BUFFER_END(HPerInstanceProp)
*/
cbuffer HPerObject
{
    int InstanceID;
};

Varyings vert( Attributes IN)
{
    Varyings OUT;
    
    //
    OUT.posW = mul(UNITY_MATRIX_M, IN.posL);
    OUT.posH = mul(UNITY_MATRIX_MVP, IN.posL);
    OUT.normalW = mul((float3x3)UNITY_MATRIX_M, IN.normalL);
    OUT.texC = IN.texC;
    OUT.vID = IN.vID;
    
    /*
    UNITY_SETUP_INSTANCE_ID(IN);
    UNITY_TRANSFER_INSTANCE_ID(IN, OUT);
    */
    return OUT;
}

uint frag(Varyings IN, uint pID : SV_PrimitiveID) : SV_Target
{
    //UNITY_SETUP_INSTANCE_ID(IN);

    return (pID << INSTANCE_ID_BITS) | InstanceID;
    /*
    half4 idpack;
    idpack.x = ((pID & 0xff00) >> 8) / 256.0;
    idpack.y = (pID & 0xff) / 256.0;
    idpack.z = ((InstanceID & 0xff00) >> 8) / 256.0;
    idpack.w = (InstanceID & 0xff) / 256.0;
    return idpack;
    */
    //return half4(pID / 2048.0, InstanceID / 32.0, 0, 1);
//
}



#endif